package cn.explink.dop.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.OrderVO;
import cn.explink.dop.entity.vo.ReturnVO;
import cn.explink.dop.service.DeliveryManService;
import cn.explink.dop.service.OrderService;
import cn.explink.dop.service.ReturnService;
import cn.explink.dop.tools.Tools;
import cn.explink.eap.core.common.model.json.AjaxJson;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;

/**
 *
 * @author wangzy
 */
@Controller
@RequestMapping("/settleAccoutsController")
public class SettleAccountsController extends CgAutoListController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private ReturnService returnService;

	@Autowired
	private DeliveryManService deliveryManService;

	/**
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "index1")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/settleAccounts/settleAccounts");
		// TODO：查询小件员
		List<Map<String, Object>> deliveryManList = this.deliveryManService.getOpenDeliveryMan();
		// Map<String, Object> map =
		// this.deliveryManService.getListDataByPage(new DeliveryManVO(), 1,
		// 10);
		String date = Tools.getDateFromToday(0);
		view.addObject("date", date);
		view.addObject("manList", deliveryManList);
		// 打印到前台
		return view;
	}

	/**
	 * 获取数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "loadData")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<OrderVO> loadData(int pageNum, int pageSize, HttpServletRequest request) {
		Map<String, Object> map = this.orderService.getListDataByPage(new OrderVO(), null, pageNum, pageSize);
		return (List<OrderVO>) map.get("list");
	}

	/**
	 *
	 * @param orderVO
	 * @param pageNum
	 * @param pageSize
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(params = "query")
	@ResponseBody
	public Map<String, Object> queryData(int pageNum, int pageSize, HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {

		String person = request.getParameter("deliveryMan");
		String deliveryTime = request.getParameter("deliveryTime");
		ReturnVO returnVO = new ReturnVO();
		returnVO.setSendPersonName(person);
		SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
		Date newdate = null;
		if ("" != deliveryTime) {
			newdate = sp.parse(deliveryTime);
		}

		returnVO.setCreateTime(newdate);
		return this.returnService.queryReturnForSettle(returnVO, pageNum, pageSize);
	}

	/**
	 *
	 * @Title: getReturnSettle
	 * @Description: 统计派送费用
	 * @author 孟凡岭
	 * @param @param deliveryManId
	 * @param @param startTime
	 * @param @param endTime
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @param request
	 * @param @return
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping(params = "queryReturn")
	@ResponseBody
	public Map<String, Object> getReturnSettle(String deliveryManId, String startTime, String endTime, int pageNum, int pageSize, HttpServletRequest request) {
		Map<String, Object> map = this.orderService.countDeliveryCharge(deliveryManId, startTime, endTime, pageNum, pageSize);
		return map;
	}

	/**
	 *
	 * @Title: getSettleDetail
	 * @Description: 获取派费详情
	 * @author 孟凡岭
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @param deliveryManId
	 * @param @param returnDate
	 * @param @param request
	 * @param @return
	 * @param @throws Exception
	 * @return List<OrderVO> 返回类型
	 * @throws
	 */
	@RequestMapping(params = "getSettleDetail")
	@ResponseBody
	public Map<String, Object> getSettleDetail(int pageNum, int pageSize, String deliveryManId, String returnDate, HttpServletRequest request) throws Exception {
		return this.orderService.getSettleDetail(pageNum, pageSize, deliveryManId, returnDate);
	}

	/**
	 *
	 * @Title: settleMoney
	 * @Description: 结算派费
	 * @author 孟凡岭
	 * @param @param deliveryIds
	 * @param @param completeTimes
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "settleMoney")
	@ResponseBody
	public AjaxJson settleMoney(@RequestParam(value = "deliveryIds[]", required = false) List<String> deliveryIds,
			@RequestParam(value = "completeTimes[]", required = false) List<String> completeTimes, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.returnService.settleMoney(deliveryIds, completeTimes);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}

	@RequestMapping(params = "check")
	@ResponseBody
	public Map<String, Object> check(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String billno = request.getParameter("billNo");
		int count = this.returnService.updateReturnStatus(billno);
		Map<String, Object> map = new HashMap<String, Object>();
		if (count > 0) {
			map = this.returnService.queryReturnByBillNo(billno);
		}
		return map;
	}

	@RequestMapping(params = "deleteReturn")
	@ResponseBody
	public Map<String, Object> deleteReturn(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String billno = request.getParameter("billNo");
		this.returnService.deleteReturn(billno);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", new ArrayList<ReturnVO>());
		map.put("count", 0);
		return map;
	}

	@RequestMapping(params = "queryDetail")
	@ResponseBody
	public Map<String, Object> queryDetailData(int pageNum, int pageSize, HttpServletRequest request, HttpServletResponse response) throws IOException {

		String returnId = request.getParameter("ReturnId");
		String id = request.getParameter("ReturnId");
		Map<String, Object> map = this.orderService.getOrderList(id, pageNum, pageSize);
		return map;
	}

	@RequestMapping(params = "deleteOrder")
	@ResponseBody
	public Map<String, Object> deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String returnid = request.getParameter("ReturnId");
		int count = this.returnService.deleteOrder(returnid);
		Map<String, Object> map = new HashMap<String, Object>();
		// OrderVO orderVO = new OrderVO();
		List<OrderVO> list = new ArrayList<OrderVO>();
		map.put("list", list);
		map.put("count", 0);
		if (count > 0) {
			return map;
		}
		return map;
		// Map<String, Object> map = new HashMap<String, Object>();
		// List<TempOrderVO> list = new ArrayList<TempOrderVO>();
		// map.put("count", 0);
		// map.put("list", list);
		//
		// return map;
	}

	/**
	 *
	 * @param status
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getOrderListByStatus")
	@ResponseBody
	public Map<String, Object> getOrderListByStatus(@RequestParam("statusList[]") List<Integer> statusList, int pageNum, int pageSize, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if ((null == statusList) || (statusList.size() == 0)) {
			return resultMap;
		}

		// TODO 分页 不过3秒刷新一次，估计分页用处不大
		Integer count = this.orderService.getCountByStatus(null, statusList);
		List<OrderVO> orderVOList = this.orderService.getOrderVOListByStatusAndDate(null, statusList, null, null);

		resultMap.put("count", count);
		resultMap.put("list", orderVOList);
		return resultMap;
	}
}
